package com.core.website.api;

import com.core.website.common.Result;
import com.core.website.common.ResultUtil;
import com.core.website.common.TableDataInfo;
import com.core.website.entity.Content;
import com.core.website.entity.Organization;
import com.core.website.entity.OrganizationUserCascadeDto;
import com.core.website.service.OrganizationService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/organizationApi")
public class OrganizationApi {
    @Autowired
    private OrganizationService organizationService;
    @PostMapping("/add")
    public Result add(@RequestBody Organization organization){
        int result=organizationService.add(organization);
        return ResultUtil.success(result);
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody Organization organization){
        int result=organizationService.edit(organization);
        return ResultUtil.success(result);
    }
    @PostMapping("/delete")
    public Result delete(@RequestBody List<String> ids){
        String name=organizationService.checkUse(ids);
        if(StringUtils.isNotEmpty(name)){
            return ResultUtil.error(-1,"机构"+name+"下已有用户，不能删除!");
        }
        int result=organizationService.delete(ids);
        if(-1==result){
            return ResultUtil.error(-1,"被删除的机构存在下级，请先删除下级机构!");
        }
        return ResultUtil.success(result);
    }
    @PostMapping("/queryAll")
    public Result queryAll(@RequestBody Organization organization){
        List<Organization> result=organizationService.queryAll(organization);
        return ResultUtil.success(result);
    }
    @PostMapping("/queryPage")
    public TableDataInfo queryPage(@RequestBody Organization organization){
        PageHelper.startPage(organization.getPageNum(), organization.getPageSize());
        List<Organization> result=organizationService.queryAll(organization);
        return TableDataInfo.getDataTable(result);
    }
    @GetMapping("/queryStatistic")
    public Result queryStatistic(){
        List<Organization> result=organizationService.queryStatistic();
        return ResultUtil.success(result);
    }
    @GetMapping("/queryOne")
    public Result queryOne(String id){
        Organization result=organizationService.queryOne(id);
        return ResultUtil.success(result);
    }
    @GetMapping("/queryCascadeUser")
    public Result queryCascadeUser(String userId){
        List<OrganizationUserCascadeDto> result=organizationService.queryCascadeUser(userId);
        return ResultUtil.success(result);
    }
}
